import java.sql.*;

public class Main {
    public static void main(String[] args) throws SQLException {
        Connection connection =null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;




        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //创建连接
            String URL = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(URL, "root", "123456");
            /*if (connection != null) {
                System.out.println("连接成功");
            }*/
            //sql
            String sql = "select * from user1";
            // 预编译SQL
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 执行sql
            ResultSet resultSet = preparedStatement.executeQuery();
            //遍历结果
            while(resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"---"+name+"---"+pass);
            }



        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            connection.close();
            preparedStatement.close();
            resultSet.close();



        }
    }
}